<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>登录</title>
  <link rel="stylesheet" href="./css/bootstrap.css">
  <style>
  h1{
    /*text-align: center;*/
  }
  </style>
  <script src="./js/jq.js"></script>
  <script src="./js/bootstrap.js"></script>
  <script>
  function login(){
    const username = $("#username").val()
    const password = $("#password").val()
    let formData = new FormData();
    formData.append("username",username)
    formData.append("password",password);
    $.ajax({
      url: 'http://localhost:8081/weibo/user/login',
      type: 'POST',
      data: formData,
      processData: false,
      contentType: false,
      success: function(response) {
        if (response.code==200) {
          alert('登录成功');
          let loginUser = response.data
          let jsonString = JSON.stringify(loginUser);
          sessionStorage.setItem('loginUser', jsonString);
          sessionStorage.setItem('token',loginUser.token)
          location.href="index.html"
        } else {
          alert('登录失败：' + response.msg);
        }
      },
      error: function() {
        alert('登录失败，请稍后重试');
      }
    });
  }
  </script>
</head>
<body>
<div class="container">
  <div class="page-header">
    <h1><a href="index.html">博客jquery</a>  <small>登录</small></h1>
  </div>
  <form class="form-horizontal">
    <div class="form-group">
      <label for="username" class="col-sm-2 control-label">username:</label>
      <div class="col-sm-10">
        <input  class="form-control" id="username" placeholder="用户名">
      </div>
    </div>
    <div class="form-group">
      <label for="password" class="col-sm-2 control-label">Password</label>
      <div class="col-sm-10">
        <input type="password" class="form-control" id="password" placeholder="密码">
      </div>
    </div>
    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
        <div class="checkbox">
          <label>
            <input type="checkbox"> Remember me
          </label>
        </div>
      </div>
    </div>
    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
        <button type="button" class="btn btn-default" onclick="login()">Sign in</button>
      </div>
    </div>
  </form>
</div>
</body>
</html>